// ==========================================================================
// GFast自动生成controller操作代码。
// 生成日期：2024-11-11 20:52:34
// 生成路径: internal/app/system/controller/users.go
// 生成人：王中阳
// desc:用户
// company:云南奇讯科技有限公司
// ==========================================================================

package controller

import (
	"context"
	"errors"

	"github.com/gogf/gf/v2/encoding/gurl"
	"github.com/gogf/gf/v2/net/ghttp"
	"github.com/tiger1103/gfast/v3/api/v1/system"
	"github.com/tiger1103/gfast/v3/internal/app/system/model"
	"github.com/tiger1103/gfast/v3/internal/app/system/service"
	"github.com/tiger1103/gfast/v3/library/libUtils"
	"github.com/xuri/excelize/v2"
)

type usersController struct {
	BaseController
}

var Users = new(usersController)

// List 列表
func (c *usersController) List(ctx context.Context, req *system.UsersSearchReq) (res *system.UsersSearchRes, err error) {
	res = new(system.UsersSearchRes)
	res.UsersSearchRes, err = service.Users().List(ctx, &req.UsersSearchReq)
	return
}

// Export 导出excel
func (c *usersController) Export(ctx context.Context, req *system.UsersExportReq) (res *system.UsersExportRes, err error) {
	var (
		r        = ghttp.RequestFromCtx(ctx)
		listData []*model.UsersInfoRes
		//表头
		tableHead = []interface{}{"", "用户名", "手机号", "是否注册跑手，0表示否，1表示是", "微信授权登录获取的唯一标识", "微信多平台唯一标识，用于在不同微信平台间识别同一用户", "性别，1表示男，2表示女，0表示未知", "国家", "省份", "城市", "用户头像的URL地址", "是否被封禁，0表示未被封禁，1表示被封禁", "用户积分", "学校ID", "是否首次登录", "支付二维码的存储路径", "钱包余额", "抽奖总金币", "钱包余额", "抽奖总次数", "创建时间", "更新时间", "删除时间"}
		excelData [][]interface{}
		//字典选项处理
	)
	req.PageNum = 1
	req.PageSize = 500
	//获取字典数据
	excelData = append(excelData, tableHead)
	for {
		listData, err = service.Users().GetExportData(ctx, &req.UsersSearchReq)
		if err != nil {
			return
		}
		if listData == nil {
			break
		}
		for _, v := range listData {
			var (
				//单选-学校ID
				schoolId string
			)
			//关联表-单选-学校ID
			if v.LinkedSchoolId != nil {
				schoolId = v.LinkedSchoolId.Schoolname
			}
			dt := []interface{}{
				v.Id,
				v.Username,
				v.Phone,
				v.UserType,
				v.Openid,
				v.Unionid,
				v.Gender,
				v.Country,
				v.Province,
				v.City,
				v.AvatarUrl,
				v.IsBan,
				v.Score,
				//关联表-单选-学校ID
				schoolId,
				v.IsFirst,
				v.PayQrCodePath,
				v.Wallet,
				v.Diamonds,
				v.Balance,
				v.TotalLotteryCount,
				v.CreatedAt.Format("Y-m-d H:i:s"),
				v.UpdatedAt.Format("Y-m-d H:i:s"),
				v.DeletedAt.Format("Y-m-d H:i:s"),
			}
			excelData = append(excelData, dt)
		}
		req.PageNum++
	}
	//创建excel处理对象
	excel := new(libUtils.ExcelHelper).CreateFile()
	defer excel.Close()
	excel.ArrToExcel("Sheet1", "A1", excelData)
	col, _ := excelize.ColumnNumberToName(len(tableHead))
	row := len(excelData)
	cr, _ := excelize.JoinCellName(col, row)
	excel.SetCellBorder("Sheet1", "A1", cr)
	_, err = excel.WriteTo(r.Response.BufferWriter)
	if err != nil {
		return
	}
	r.Response.Header().Set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
	r.Response.Header().Set("Accept-Ranges", "bytes")
	r.Response.Header().Set("Access-Control-Expose-Headers", "*")
	r.Response.Header().Set("Content-Disposition", "attachment; filename="+gurl.Encode("C端用户")+".xlsx")
	r.Response.Buffer()
	r.Exit()
	return
}

// LinkedUsersDataSearch 相关连表查询数据
func (c *usersController) LinkedUsersDataSearch(ctx context.Context, req *system.LinkedUsersDataSearchReq) (res *system.LinkedUsersDataSearchRes, err error) {
	if !service.SysUser().AccessRule(ctx, service.Context().GetUserId(ctx), "api/v1/system/users/list") {
		err = errors.New("没有访问权限")
		return
	}
	res = new(system.LinkedUsersDataSearchRes)
	res.LinkedUsersDataSearchRes, err = service.Users().LinkedUsersDataSearch(ctx)
	return
}

// Get 获取用户
func (c *usersController) Get(ctx context.Context, req *system.UsersGetReq) (res *system.UsersGetRes, err error) {
	res = new(system.UsersGetRes)
	res.UsersInfoRes, err = service.Users().GetById(ctx, req.Id)
	return
}

// Add 添加用户
func (c *usersController) Add(ctx context.Context, req *system.UsersAddReq) (res *system.UsersAddRes, err error) {
	err = service.Users().Add(ctx, req.UsersAddReq)
	return
}

// Edit 修改用户
func (c *usersController) Edit(ctx context.Context, req *system.UsersEditReq) (res *system.UsersEditRes, err error) {
	err = service.Users().Edit(ctx, req.UsersEditReq)
	return
}

// Delete 删除用户
func (c *usersController) Delete(ctx context.Context, req *system.UsersDeleteReq) (res *system.UsersDeleteRes, err error) {
	err = service.Users().Delete(ctx, req.Ids)
	return
}
